# dnl Process this file with autoconf to create configure.

# 初始化autotool & autoconf 项目
AC_INIT([scrot], [1.12.1],
        [https://github.com/resurrecting-open-source-projects/scrot/issues],,
        [https://github.com/resurrecting-open-source-projects/scrot])
# 源码文件
AC_CONFIG_SRCDIR([src/scrot.c])
# 初始化automake
AM_INIT_AUTOMAKE

# Checks for programs.
orig_CFLAGS="${CFLAGS}" # Save CFLAGS before AC_PROG_CC sets them.
AC_PROG_CC
# In autoconf < 2.70, AC_PROG_CC determines the compiler, and AC_PROG_CC_STDC
# sets it to the newest C standard supported by autoconf.
# In autoconf >= 2.70, AC_PROG_CC determines the compiler and sets it to the
# newest C standard supported by autoconf, and AC_PROG_CC_STDC is deprecated.
m4_version_prereq([2.70],, [AC_PROG_CC_STDC])
AC_PROG_INSTALL
AC_PROG_MAKE_SET

AM_MAINTAINER_MODE

m4_pattern_forbid([^AX_],[=> GNU autoconf-archive not present. <=])
AS_IF([test "x$orig_CFLAGS" = "x"], [
    CFLAGS=""
    AX_APPEND_COMPILE_FLAGS(["-flto"])
    AS_IF([test "x$CFLAGS" = "x-flto"], [
        LTO_ENABLED=yes
        AX_APPEND_LINK_FLAGS(["-flto"])
    ])
    m4_foreach([SCROT_FLAG],
        [["-O3"], ["-Wall"], ["-Wextra"], ["-Wpedantic"]], [
            AX_APPEND_COMPILE_FLAGS(["SCROT_FLAG"])
            AS_IF([test "x$LTO_ENABLED" = "xyes"], [
                AX_APPEND_LINK_FLAGS(["SCROT_FLAG"])
            ])
        ]
    )
    # SCROT_PRIVATE_FLAGS are mainly used by the CI to append additional flags.
    # append them unconditionally, the CI shouldn't try to add flags the
    # compiler doesn't support.
    AC_SUBST([CFLAGS], ["$CFLAGS $SCROT_PRIVATE_FLAGS"])
    AS_IF([test "x$LTO_ENABLED" = "xyes"], [
        AC_SUBST([LDFLAGS], ["$LDFLAGS $SCROT_PRIVATE_FLAGS"])
    ])
])

# Checks for libraries.
m4_ifndef([PKG_PREREQ],
     [m4_fatal([either pkg-config or pkg.m4 is not installed])])
# 检查deps.pc 文件作为本项目的依赖
PKG_CHECK_MODULES([SCROT_DEPS], ["$srcdir/deps.pc"])

# TODO: When -Wpedantic and -Werror are both set:
# - header checks fail due to empty translation unit:
#   https://github.com/resurrecting-open-source-projects/scrot/pull/333#issuecomment-1572157050
# - functions checks fail due to -Wstrict-prototypes:
#   https://github.com/resurrecting-open-source-projects/scrot/pull/356#issuecomment-1608585396
# figure out a way to fix these and remove `-Wno-error=pedantic` from the CI.
# 使用到BSD
AC_CHECK_FUNCS([err errx warn warnx],, [LIBBSD_NEEDED=yes])
# 检查BSD的依赖
AS_IF([test "x$LIBBSD_NEEDED" = "xyes"], [
    PKG_CHECK_MODULES([LIBBSD], [libbsd-overlay],,
        [AC_MSG_ERROR([BSD functions not found, libbsd is required])])
])

# 替换 LIBS 变量
AC_SUBST([LIBS], ["$SCROT_DEPS_LIBS $LIBBSD_LIBS $LIBS"])
# 替换 CPPFLAGS 变量
AC_SUBST([CPPFLAGS], ["$SCROT_DEPS_CFLAGS $LIBBSD_CFLAGS $CPPFLAGS"])
# 找到本目录的Makefile和src/Makefile， 可能是从Makefile.am文件产生
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
